package com.sywb.chuangyebao.view.dialog;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.utils.Utils;

import java.io.Serializable;

import butterknife.BindView;
import butterknife.OnClick;

public class ReportOrShieldDialog extends BaseDialogFragment {

    @BindView(R.id.tv_report)
    TextView tvReport;
    @BindView(R.id.tv_shield)
    TextView tvShield;
    @BindView(R.id.tv_cancel)
    TextView tvCancel;

    private int uid;
    private String intro;

    public static ReportOrShieldDialog newInstance(Object... pramars) {
        ReportOrShieldDialog dialogFragment = new ReportOrShieldDialog();
        dialogFragment.setArguments(getBundle(pramars));
        return dialogFragment;
    }

    @Override
    public int getLayoutId(Bundle bundle) {
        return R.layout.dialog_report_or_shield;
    }

    @Override
    public void initView(View view, Bundle bundle) {
        if (bundle != null) {
            uid = bundle.getInt("p0", 0);
            intro = bundle.getString("p1");
        } else {
            uid = getArguments().getInt("p0", 0);
            intro = getArguments().getString("p1");
        }
    }

    @OnClick({R.id.tv_report, R.id.tv_shield, R.id.tv_cancel})
    @Override
    public void onClick(View v) {
        if (v.getId() != R.id.tv_cancel) {
            if (!Utils.checkIsNext(getMyFragmentManager())) {
                return;
            }
            switch (v.getId()) {
                case R.id.tv_report:
                    report();
                    break;
                case R.id.tv_shield:
                    shield();
                    break;
            }
        }
        exit();
    }

    public void report() {
        HttpUtils.report(Constants.TOPIC_TYPE_USER, uid, 1, new HttpCallback<Object>() {
            @Override
            public void onSuccess(Object o) {
                if (onItemListener != null) {
                    onItemListener.onResult(1, true, "举报成功");
                }
            }

            @Override
            public void onError(String strMsg) {
                super.onError(strMsg);
                if (onItemListener != null) {
                    onItemListener.onResult(1, false, strMsg);
                }
            }
        });
    }

    public void shield() {
        HttpUtils.shield(uid, new HttpCallback<Object>() {
            @Override
            public void onSuccess(Object o) {
                if (onItemListener != null) {
                    onItemListener.onResult(2, true, "拉黑成功");
                }
            }

            @Override
            public void onError(String strMsg) {
                super.onError(strMsg);
                if (onItemListener != null) {
                    onItemListener.onResult(2, false, strMsg);
                }
            }
        });
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("p0", uid);
        outState.putString("p1", intro);
        super.onSaveInstanceState(outState);
    }

    @Override
    public boolean isMatchParent() {
        return true;
    }

    private OnItemListener onItemListener;

    public interface OnItemListener {
        void onResult(int postion, boolean isSuccess, String message);
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }
}
